﻿using System;
using System.Collections;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;

namespace CMS.Data
{
    public class ChucNangDao : AbstractNHibernateDao<ChucNang, System.Int32>, IChucNangDao
    {
        #region Create Instance
        private static ChucNangDao instance = null;
        public static ChucNangDao Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ChucNangDao();
                }
                return instance;
            }
        }
        #endregion

        #region IChucNangDao Members

        public List<ChucNang> GetChucNangMenuNgang()
        {
            try
            {
                String strQuery = @"From ChucNang f Where f.MaCapCha = 0  Order by f.ThuTu";
                List<ChucNang> Result = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                        .List<ChucNang>() as List<ChucNang>;
                return Result;
            }
            catch
            {
                return new List<ChucNang>();
            }  
        }

        private List<ChucNang> GetChucNangOfNSD(string maNguoiSuDung)
        {
            try
            {

                int MaNhom = 0;
                List<ChucNang> ChucNangNhom = new List<ChucNang>();
                NguoiSuDung NSD = NguoiSuDungDao.Instance.GetById(maNguoiSuDung);
                if (NSD != null)
                {
                    MaNhom = NSD.MaNhom;
                    if (MaNhom != 0)
                    {
                        String strQR = @"Select pn.ChucNang From PhanQuyenNhom pn Where  pn.ID.MaNhom =:MaNhom Order by pn.ChucNang.ThuTu";
                        ChucNangNhom = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQR)
                            .SetParameter("MaNhom", MaNhom)
                            .List<ChucNang>() as List<ChucNang>;
                    }
                }

                String strQuery = @"Select pc.ChucNang From PhanQuyenNguoiSuDung pc Where  pc.ID.MaNguoiSuDung =:maNguoiSuDung Order by pc.ChucNang.ThuTu";
                List<ChucNang> Result = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                    .SetParameter("maNguoiSuDung", maNguoiSuDung)
                    .List<ChucNang>() as List<ChucNang>;

                if (Result != null)
                {
                    foreach (ChucNang item in ChucNangNhom)
                    {
                        if (Result.IndexOf(item) == -1)
                            Result.Add(item);
                    }
                }
                Result.Sort(delegate(ChucNang c1, ChucNang c2) { return c1.ThuTu.CompareTo(c2.ThuTu);});
                return Result;
            }
            catch (Exception ex)
            {
                return new List<ChucNang>();
            }  
            
        }

        public IList GetAllChucNang(string maNguoiSuDung)
        {
          try
            {
                List<ChucNang> listChucNangOfNSD =   this.GetChucNangOfNSD(maNguoiSuDung);
                //List<ChucNang> listAll = ChucNangDao.Instance.GetAll(); 
                List<ChucNang> listAll = ChucNangDao.Instance.GetAllWithMoreFieldsAndOderBy(true, new string[] { "TrangThai" }, new object[] { true }, new string[] { "ThuTu" }, true);
  
                IList Result = new ArrayList();
                List<ChucNang> listMenuNgang = null;
                List<ChucNang> listMenuDoc = null;
                List<ChucNang> listMenuItem = null;
                listMenuNgang = listAll.FindAll(delegate(ChucNang cn) { return cn.MaCapCha == 0; });
                listMenuNgang.Sort(delegate(ChucNang c1, ChucNang c2) { return c1.ThuTu.CompareTo(c2.ThuTu); });
                foreach (ChucNang menungang in listMenuNgang)
                {
                    //TrangThai = True (Chức năng này sẽ không cần phân quyền => IsQuyen = False)
                    if (listChucNangOfNSD.IndexOf(menungang) != -1 || menungang.TrangThai == false)
                        menungang.IsQuyen = true;

                    IList itemMenuDoc = new ArrayList();
                    IList<ChucNang> menuCap1 = new List<ChucNang>();
                    listMenuDoc = listAll.FindAll(delegate(ChucNang cn) { return cn.MaCapCha == menungang.ID; });
                    listMenuDoc.Sort(delegate(ChucNang c1, ChucNang c2) { return c1.ThuTu.CompareTo(c2.ThuTu); });
                    foreach (ChucNang menudoc in listMenuDoc)
                    {
                        menudoc.NodeNumber = 0;
                        menuCap1.Add(menudoc);
                        //TrangThai = True (Chức năng chính của menu dọc sẽ không cần phân quyền => IsQuyen = False)
                        if (listChucNangOfNSD.IndexOf(menudoc) != -1 || menudoc.TrangThai==false)
                            menudoc.IsQuyen = true;
                        //if (menudoc.IsRoot)
                        //{
                        listMenuItem = listAll.FindAll(delegate(ChucNang cn) { return cn.MaCapCha == menudoc.ID; });
                        listMenuItem.Sort(delegate(ChucNang c1, ChucNang c2) { return c1.ThuTu.CompareTo(c2.ThuTu); });
                        foreach (ChucNang menuItem in listMenuItem)
                        {
                            if (listChucNangOfNSD.IndexOf(menuItem) != -1 || menuItem.TrangThai == false)
                                menuItem.IsQuyen = true;

                            menuItem.NodeNumber = 1;
                            menuCap1.Add(menuItem);
                        }
                        //}
                    }
                    itemMenuDoc.Add(menungang);
                    itemMenuDoc.Add(menuCap1);                    
                    Result.Add(itemMenuDoc);
                }
                return Result;
            }
            catch(Exception ex)
            {
                return new ArrayList();
            }  
        }

        public IList GetAllViews()
        {
            try
            {
                //List<ChucNang> listChucNangOfNSD = this.GetChucNangOfNSD(maNguoiSuDung);
                List<ChucNang> listAll = ChucNangDao.Instance.GetAllWithMoreFieldsAndOderBy(true, new string[] { "TrangThai" }, new object[] { true }, new string[] { "ThuTu" }, true);

                IList Result = new ArrayList();
                List<ChucNang> listMenuNgang = null;
                List<ChucNang> listMenuDoc = null;
                List<ChucNang> listMenuItem = null;
                listMenuNgang = listAll.FindAll(delegate(ChucNang cn) { return cn.MaCapCha == 0; });
                listMenuNgang.Sort(delegate(ChucNang c1, ChucNang c2) { return c1.ThuTu.CompareTo(c2.ThuTu); });
                foreach (ChucNang menungang in listMenuNgang)
                {
                    menungang.IsQuyen = true;   
                    IList itemMenuDoc = new ArrayList();
                    IList<ChucNang> menuCap1 = new List<ChucNang>();
                    listMenuDoc = listAll.FindAll(delegate(ChucNang cn) { return cn.MaCapCha == menungang.ID; });
                    listMenuDoc.Sort(delegate(ChucNang c1, ChucNang c2) { return c1.ThuTu.CompareTo(c2.ThuTu); });
                    foreach (ChucNang menudoc in listMenuDoc)
                    {
                        menudoc.IsQuyen = true;
                        menudoc.NodeNumber = 0;
                        menuCap1.Add(menudoc);

                        //if (menudoc.IsRoot)
                        //{
                        listMenuItem = listAll.FindAll(delegate(ChucNang cn) { return cn.MaCapCha == menudoc.ID; });
                        listMenuItem.Sort(delegate(ChucNang c1, ChucNang c2) { return c1.ThuTu.CompareTo(c2.ThuTu); });
                        foreach (ChucNang menuItem in listMenuItem)
                        {
                            //if (listChucNangOfNSD.IndexOf(menuItem) != -1 || menuItem.TrangThai == false)
                            //    menuItem.IsQuyen = true;
                            menuItem.IsQuyen = true;
                            menuItem.NodeNumber = 1;
                            menuCap1.Add(menuItem);
                        }
                        //}
                    }
                    itemMenuDoc.Add(menungang);
                    itemMenuDoc.Add(menuCap1);
                    Result.Add(itemMenuDoc);
                }
                return Result;
            }
            catch
            {
                return new ArrayList();
            }
        }

        public List<ChucNang> GetChucNangOfNhomNguoiSuDung(int maNhom)
        {
            try
            {
                String strQuery = @"Select pn.ChucNang From PhanQuyenNhom pn Where  pn.ID.MaNhom =:maNhom Order by pn.ChucNang.ThuTu";
                List<ChucNang> Result = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                    .SetParameter("maNhom", maNhom)
                    .List<ChucNang>() as List<ChucNang>;
                return Result;
            }
            catch (Exception ex)
            {
                return new List<ChucNang>();
            }  
        }

        public List<ChucNang> GetChucNangKhongThuocNhom(int maNhom)
        {
            try
            {
                String strQuery = @"Select From ChucNang cn Where cn.TrangThai=1 And cn.ID NOT IN (Select pn.ID.MaChucNang From PhanQuyenNhom pn Where pn.ID.MaNhom=:maNhom) Order by cn.ThuTu";
                List<ChucNang> Result = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                    .SetParameter("maNhom", maNhom)
                    .List<ChucNang>() as List<ChucNang>;
                return Result;
            }
            catch (Exception ex)
            {
                return new List<ChucNang>();
            }  
        }

        #endregion
    }
}
